//
//  MyAccountViewController.m
//  Kjs
//
//  Created by cfq on 16/4/17.
//  Copyright © 2016年 kjs. All rights reserved.
//

#import "MyAccountViewController.h"
#import "MyAccountFuctionCell.h"
#import "MyAccountHeaderView.h"
#import "AccountDetailViewController.h"
#import "SecuritySetViewController.h"
#import "InvestManageViewController.h"
#import "RaiseManageViewController.h"
#import "FinanceLogViewController.h"
#import "AccountOverviewViewController.h"
#import "UserVirtualAccountModel.h"
#import "KJSRequest.h"
#import "AppDelegate.h"
#import <MJRefresh.h>

@interface MyAccountViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) MyAccountHeaderView* myAccountHV;
@property (strong, nonatomic) UserVirtualAccountModel *userVirtualAccountModel;
@property (nonatomic,strong) NSString *partyId;
@end

@implementation MyAccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"账户";
    self.naviTopView = YES;
    self.tableView.bounces = YES;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
//    self.tableView.tableFooterView = [[[NSBundle mainBundle] loadNibNamed:@"MyAccountFooterView" owner:self options:nil] firstObject];
    self.myAccountHV  = [[[NSBundle mainBundle] loadNibNamed:@"MyAccountHeaderView" owner:self options:nil] firstObject];
    UITapGestureRecognizer *goAccountDetail = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goAccountDetail) ];
    [self.myAccountHV.arrowImageView addGestureRecognizer:goAccountDetail];
    self.myAccountHV.arrowImageView.userInteractionEnabled = YES;
    self.tableView.tableHeaderView = self.myAccountHV;
    
    [self GetUserVirtualAssets];
    NSLog(@"加载");
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"tilte= %@",self.myAccountHV.nickName);
    if (CurentDelegate.account && [self.myAccountHV.userNameLab.text isEqualToString:@"未登录"]) {
        self.tableView.bounces = YES;
        [KJSRequest GetUserAccountAssetsInfoWithPartyId:CurentDelegate.account.partyId ret:^(UserVirtualAccountModel *model) {
            self.myAccountHV.userAssets = model.userVirtualAccount;
            self.myAccountHV.nickName = CurentDelegate.account.nickName;
            self.userVirtualAccountModel = model;
            [self endRefresh];
            [self.tableView reloadData];
        }];
    }else if (!CurentDelegate.account){
        self.tableView.bounces = NO;
        self.myAccountHV.userAssets = [[UserAssets alloc] init];
        self.myAccountHV.nickName = NULL;
        [self.tableView reloadData];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (CurentDelegate.account) {
        if (![CurentDelegate.account.partyId isEqualToString:self.partyId]) {
            [self GetUserVirtualAssets];
        }
    }
}
#pragma mark - custom method
- (void)GetUserVirtualAssets
{
    if (CurentDelegate.account) {
        [KJSRequest GetUserAccountAssetsInfoWithPartyId:CurentDelegate.account.partyId ret:^(UserVirtualAccountModel *model) {
            self.myAccountHV.userAssets = model.userVirtualAccount;
            self.myAccountHV.nickName = CurentDelegate.account.nickName;
            self.userVirtualAccountModel = model;
            self.partyId = CurentDelegate.account.partyId;
            [self endRefresh];
            [self.tableView reloadData];
        }];
    }else{
        self.myAccountHV.userAssets = [[UserAssets alloc] init];
        self.myAccountHV.nickName = NULL;
        [self.tableView reloadData];
    }
    
}

- (void)goAccountDetail
{
    NSLog(@"跳转到详情");
    AccountDetailViewController *accountDetailVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"AccountDetailViewController"];
    accountDetailVC.loginOutSuccess = ^(void){
        [self GetUserVirtualAssets];
    };
   
    [self displayViewController:accountDetailVC];
    
}
#pragma mark - UITableViewSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyAccountFuctionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyAccountFuctionCell"];
    cell.type = indexPath.row;
    //判断是不是section的第0个cell，如果是，在cell顶部加上分割线
    cell.firstCell = !indexPath.row;
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if(indexPath.row) {
        return 45;
//    }
//    else {
//        return 65;
//    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case AccountOverview:
        {
            NSLog(@"跳转到账户总览");
            AccountOverviewViewController *accountOverviewVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"AccountOverviewViewController"];
            accountOverviewVC.userVirtualAccountModel = self.userVirtualAccountModel;
            [self displayViewController:accountOverviewVC];

        }
            break;
        case InvestmentManagement:
        {
            NSLog(@"跳转投资管理");
            InvestManageViewController *investManageVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"InvestManageViewController"];
            [self displayViewController:investManageVC];
        }
            break;
        case FinancingManagement:
        {
            NSLog(@"融资管理");
            RaiseManageViewController *raiseManageVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"RaiseManageViewController"];
            [self displayViewController:raiseManageVC];
            break;
        }
        
        case CapitalFlow:
        {
            NSLog(@"资金流水");
            FinanceLogViewController *financeLogVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"FinanceLogViewController"];
            [self displayViewController:financeLogVC];
        }
            break;
        case SecuritySetting:
        {
            
            SecuritySetViewController *securitySetVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"SecuritySetViewController"];
            securitySetVC.loginOutSuccess = ^(void){
                [self GetUserVirtualAssets];
            };
            //安全设置会更新一些账户首页的信息，所有返回要刷新
            securitySetVC.refreshAccountIndex = ^(){
                [self GetUserVirtualAssets];
            };
            [self displayViewController:securitySetVC];

        }
            break;
        default:
            NSLog(@"跳转异常");
            break;
    }
}
#pragma mark - overwrite method
- (void)displayViewController:(UIViewController *)viewController {
    NSLog(@"%@",CurentDelegate.account);
    if(CurentDelegate.account) {
        [super displayViewController:viewController];
    }
    else {
        [CurentDelegate getAccountMessageAt:self ret:^(UserInfo *account) {
        }];
    }
}
#pragma mark - refresh method
- (void)headerRefresh {
   [self GetUserVirtualAssets];
}

- (void)endRefresh {
    [self.tableView.mj_header endRefreshing];
}
@end
